//
//  SKXAcceptViewController.m
//  NewChat
//
//  Created by songKX on 16/3/7.
//  Copyright © 2016年 蓝鸥. All rights reserved.
//

#import "SKXAcceptViewController.h"

#import "AcceptTableViewCell.h"

#import "SystemMessage.h"

@interface SKXAcceptViewController ()<UITableViewDelegate,UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *acceptTableView;

@property (nonatomic , strong) UILabel *sysText;
@end

@implementation SKXAcceptViewController


- (void)viewDidLoad {
    [super viewDidLoad];
    self.acceptTableView.dataSource = self;
    
    self.acceptTableView.delegate = self;
    
    if ([SKXFriendManager sharaFriendManager].friendAcceptArray.count == 0&&[SKXFriendManager sharaFriendManager].historyAcceptArray.count == 0) {
        
        NSLog(@"没有更多系统通知");
        
        self.sysText = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 200, 40)];
        
        self.sysText.textAlignment = NSTextAlignmentCenter;
        
        self.sysText.center = CGPointMake([UIScreen mainScreen].bounds.size.width*0.5, [UIScreen mainScreen].bounds.size.height*0.5);
        
        self.sysText.text = [NSString stringWithFormat:@"暂无系统通知"];
        [self.view addSubview:self.sysText];
        
    }else{
    //清空消息列表并加入历史系统消息列表
    
        [[SKXFriendManager sharaFriendManager].historyAcceptArray addObjectsFromArray:[SKXFriendManager sharaFriendManager].friendAcceptArray];
        
        [[SKXFriendManager sharaFriendManager].friendAcceptArray removeAllObjects];;
        
                
    }
    
    //获取通知中心单例对象
    NSNotificationCenter * center = [NSNotificationCenter defaultCenter];
    //添加当前类对象为一个观察者，name和object设置为nil，表示接收一切通知
    [center addObserver:self selector:@selector(notice:) name:systemMessageChange object:nil];

    
    
    // Do any additional setup after loading the view from its nib.
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    AcceptTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"acCell"];
    
    SystemMessage *sys = [SKXFriendManager sharaFriendManager].historyAcceptArray[indexPath.row];
    
    
    
    if (!cell)
    {
        [tableView registerNib:[UINib nibWithNibName:@"AcceptTableViewCell" bundle:nil] forCellReuseIdentifier:@"acCell"];
        cell = [tableView dequeueReusableCellWithIdentifier:@"acCell"];
    }
    
    
    
    if (sys.systemMessageType != SystemMessageTypeFriendAccept) {
        
        cell.infoTextLabel.text = sys.message;
        
        
        NSLog(@"%@",sys.message);
        
        cell.registRequst.hidden = YES;
        cell.acceptRequst.hidden = YES;
        
    }else{
        
        cell.registRequst.tag = 200+indexPath.row;
        
        cell.acceptRequst.tag = 200+indexPath.row;
        
        
        cell.infoTextLabel.text = sys.message;
        
        [cell.acceptRequst addTarget:self action:@selector(acceptRequstAction:) forControlEvents:UIControlEventTouchUpInside];
        
        [cell.registRequst addTarget:self action:@selector(registRequstAction:) forControlEvents:UIControlEventTouchUpInside];
        
    }
    cell.headerView.image = [UIImage imageNamed:sys.accepterObject[@"userImageName"]];
    
    return cell;
    
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    
    if ([SKXFriendManager sharaFriendManager].historyAcceptArray.count != 0) {
        
        [self.sysText removeFromSuperview];
        
    }

    return [SKXFriendManager sharaFriendManager].historyAcceptArray.count;
    
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 80;
}


- (void)acceptRequstAction:(UIButton *)sender{
    
    
    SystemMessage * temp = [SKXFriendManager sharaFriendManager].historyAcceptArray[sender.tag - 200];
    
    NSIndexPath *path = [NSIndexPath indexPathForRow:sender.tag - 200 inSection:0];
    
    AcceptTableViewCell *cell = [self.acceptTableView cellForRowAtIndexPath:path];
    
    cell.registRequst.hidden = YES;
    
    cell.acceptRequst.hidden = YES;
    
    EMError *error1 = nil;
    NSLog(@"点击了接受,%@",temp.accepter);
    BOOL isSuccess1 = [[EaseMob sharedInstance].chatManager acceptBuddyRequest:temp.accepterObject[@"username"] error:&error1];
    if (isSuccess1 && !error1) {
        NSLog(@"发送同意成功");
        temp.systemMessageType = SystemMessageTypeAgreeFriendAccept;
        
        [temp setMessageTextWithType:SystemMessageTypeAgreeFriendAccept];
        
        [self.acceptTableView reloadData];
    }
    
}

- (void)registRequstAction:(UIButton *)sender{

    
    NSIndexPath *path = [NSIndexPath indexPathForRow:sender.tag - 200 inSection:0];
    
    AcceptTableViewCell *cell = [self.acceptTableView cellForRowAtIndexPath:path];
    
    cell.registRequst.hidden = YES;
    
    cell.acceptRequst.hidden = YES;

    
    NSLog(@"点击了拒绝");
    
    SystemMessage * temp = [SKXFriendManager sharaFriendManager].historyAcceptArray[sender.tag - 200];
    NSLog(@"点击了拒绝,%@",temp.accepter);
    
    EMError *error2 = nil;
        BOOL isSuccess2 = [[EaseMob sharedInstance].chatManager rejectBuddyRequest:temp.accepterObject[@"username"] reason:@"111111" error:&error2];
        if (isSuccess2 && !error2) {
            NSLog(@"发送拒绝成功");
            
            temp.systemMessageType = SystemMessageTypeDisagreeAccept;
            
            [temp setMessageTextWithType:SystemMessageTypeDisagreeAccept];
            
            [self.acceptTableView reloadData];
            
        }
}

//实现通知中心的方法

-(void)notice:(id)sender{
    
    [[SKXFriendManager sharaFriendManager].historyAcceptArray addObjectsFromArray:[SKXFriendManager sharaFriendManager].friendAcceptArray];
    
    [[SKXFriendManager sharaFriendManager].friendAcceptArray removeAllObjects];;

    [self.acceptTableView reloadData];
}

@end
